#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>

int main(int argc, char** argv) {
    int rank, size;
    int sum = 0;
    int local_sum = 0;

    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);

    // 每个进程计算部分和
    for (int i = rank + 1; i <= size; i += size) {
        local_sum += i;
    }

    // 各进程将部分和求和
    MPI_Reduce(&local_sum, &sum, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);

    // 输出结果
    if (rank == 0) {
        printf("Total sum: %d\n", sum);
    }

    MPI_Finalize();
    return 0;
}
